<template>
  <div class="message-box">
    <div class="messages">
      <div
          v-for="message in messages"
          :key="message.id"
          :class="['message', message.sender === 'me' ? 'message-me' : 'message-friend']"
      >
        {{ message.text }}
      </div>
    </div>
    <div class="input-box">
      <input
          v-model="newMessage"
          @keyup.enter="sendMessage"
          placeholder="输入消息..."
      />
      <button @click="sendMessage">发送</button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    friend: {
      type: Object,
      required: true,
    },
    messages: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      newMessage: "",
    };
  },
  methods: {
    sendMessage() {
      if (this.newMessage.trim()) {
        this.$emit("send-message", this.newMessage);
        this.newMessage = "";
      }
    },
  },
};
</script>

<style scoped>
.message-box {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  background-color: white;
  border-radius: 4px;
}

.message {
  padding: 10px;
  margin-bottom: 10px;
  border-radius: 4px;
  max-width: 70%;
}

.message-me {
  background-color: #3498db;
  color: white;
  margin-left: auto;
}

.message-friend {
  background-color: #bdc3c7;
  color: black;
  margin-right: auto;
}

.input-box {
  display: flex;
  padding: 10px 0;
}

input {
  flex: 1;
  padding: 10px;
  border: 1px solid #bdc3c7;
  border-radius: 4px;
  margin-right: 10px;
}

button {
  padding: 10px 20px;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #2980b9;
}
</style>